import requests
from fastapi.responses import ORJSONResponse, JSONResponse
from fastapi.routing import APIRouter
from extensions import *
from extensions.utils import *

router = APIRouter(
    prefix="/api",
    tags=["文档数据"],
    default_response_class=ORJSONResponse
)


def send_request(url, data):
    defaule_url = 'http://localhost:8008/api'
    headers = {
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        "Content-Type": "application/json",
    }
    try:
        response = requests.post(defaule_url+url, data=data, headers=headers)
        return response.json()
    except Exception as e:
        return e


@router.post("/rpc/task/list", summary='rpc任务列表')
async def rpc_task_list(item: dict):
    json_data = send_request('/rpc/task', json.dumps(item))
    return JSONResponse(status_code=200, content=json_data)


@router.post("/rpc/task/run", summary='rpc任务运行')
async def rpc_task_run(item: dict):
    json_data = send_request('/rpc/run', json.dumps(item))
    return JSONResponse(status_code=200, content=json_data)


@router.post("/rpc/task/stop", summary='rpc任务停止')
async def rpc_task_stop(item: dict):
    json_data = send_request('/rpc/stop', json.dumps(item))
    return JSONResponse(status_code=200, content=json_data)
